<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html class="win-frame">
<head>
<title>${name_china}管理-编辑${name_china}</title>
<%@include file="/common/head.jsp" %>

</head>
<body class="iframe-page">
<div class="page-title-wrapper">
    <div class="page-title">
        <div class="page-title-text">
            <span>${name_china}管理</span>
            <span class="split"></span>
            <span>编辑${name_china}</span>
        </div>
        <a id="goBack" class="btn-return distance-left" href="javascript:;"><i class="icon icon-arrow-left"></i>返回上级</a>
    </div>
    <div class="page-title-border"></div>
</div>

<form id="form" method="post">
    <div class="form-wrapper">
	  <div class="h4 distance-double">
        <span class="h4-title"><i class="icon icon-setting"></i>基本信息</span>
        <span class="h4-border"></span>
      </div>
      
      <input type="hidden" id="id" name="id"/> 
      
       <div class="form-cont">
	        <p class="form-cont-title">排序号</p>
	        <input type="text" id="reorder" name="reorder" class="summer-input summer-validatebox"  data-validatebox="validType:['noNegativeNumber']"/> 
	     </div>
	     
	      <div class="form-cont">
	        <p class="form-cont-title">是否可见</p>
	        <input type="text" id="isVisible" name="isVisible" class="summer-input summer-validatebox"  data-validatebox="validType:['maxLength[10]']"/> 
	     </div>
	     
	     <div class="form-cont">
	        <p class="form-cont-title">轮播图位置</p>
	        <input type="text" id="type" name="type" class="summer-input summer-validatebox"  data-validatebox="validType:['maxLength[11]']"/> 
	     </div>
	     
	     <div class="form-cont">
	        <p class="form-cont-title"><span class="field-required">*</span>轮播图名</p>
	        <input type="text" id="name" name="name" class="summer-input summer-validatebox"  data-validatebox="validType:['required','maxLength[50]']"/> 
	     </div>
	     
	     <div class="form-cont">
	        <p class="form-cont-title">轮播图片</p>
	        <input type="hidden" id="img" name="img"/>
	     </div>
	     
	     <div class="form-cont">
	        <p class="form-cont-title">活动专题的图片</p>
	        <input type="hidden" id="banner" name="banner"/>
	     </div>
	     
	      <div id="happ" class="form-cont">
	        <p class="form-cont-title"><span class="field-required">*</span>H5或APP</p>
	        <input type="text" id="linkType" name="linkType" class="summer-input summer-validatebox"  data-validatebox="validType:['required','maxLength[10]']"/> 
	     </div>
	     
	    <div class="form-cont">
	        <p class="form-cont-title">活动标题</p>
	        <input type="text" id="title" name="title" class="summer-input summer-validatebox"  data-validatebox="validType:['maxLength[10]']"/> 
	     </div>
	     
	     <div class="form-cont">
	        <p class="form-cont-title">内容</p>
	        <input type="text" id="content" name="content" class="summer-input summer-validatebox"  data-validatebox="validType:['maxLength[10]']"/> 
	     </div>
	     
	     <div class="form-cont" >
	         <p class="form-cont-title">商品选择</p>
	         <span class="field-cont">
		        <span class="field-cont-title">商品名称</span>
		        <input type="text" class="summer-input" id="goodsName"/>
		    </span>
		    <span class="field-cont">
		        <span class="field-cont-title">商品类型</span>
		        <input type="text" class="summer-input" id="spuPlatform"/>
		    </span>
		     <span class="field-cont">
		        <span class="field-cont-title">商品分类</span>
		        <input type="text" id="categoryId" name="categoryId" />  
		    </span>
		     <span class="field-cont">
		        <span class="field-cont-title">是否爆款</span>
		        <input type="text" class="summer-input" id="isHot"/>
		    </span>
		    <span class="field-cont">
		        <span class="field-cont-title">是否精品</span>
		        <input type="text" class="summer-input" id="isJingping" />
		    </span>
		     <span class="field-cont">
		        <span class="field-cont-title">佣金排序</span>
		        <input type="text" class="summer-input" id="wlCommission"/>
		    </span>
		     <span class="field-cont">
		        <span class="field-cont-title">佣金比例排序</span>
		        <input type="text" class="summer-input" id="wlCommissionShare"/>
		    </span>
		     <span class="field-cont">
		        <span class="field-cont-title">时间</span>
		        <input type="text" class="summer-input" id="createTime"/>
		    </span>
		    <span class="field-cont">
		        <span class="field-cont-title">商品</span>
		        <input type="text" class="summer-input" id="goodsCheck" />
		    </span>
		      <span class="field-btn-wrapper">
		        <a id="search" class="btn btn-primary btn-icon wave distance-right"><i class="icon icon-search"></i></a>
		        <a id="reset" class="btn btn-icon wave wave-dark"><i class="icon icon-refresh"></i></a>
		    </span>
	     </div>
	     
	      <input id="spuIds" type="hidden"  name="spuIds"/>
	      <div id="t-wrap">
		    <table class="summer-datagrid" id="dataList">
			    <thead>
				    <tr>
				        <th>商品名称</th>
				        <th>商品图片</th>
				        <th>商品类型</th>
				        <th>商品价格</th>
				        <th>商品红包</th>
				        <th>商品券后价</th>
				        <th>佣金率</th>
						<th>佣金金额</th>
				        <th>备注</th>
				        <th>操作</th>
				    </tr>
			    </thead>
		    </table>
		</div>
	     
     <div class="form-btn-wrapper" id="funcbar">
        <a class="btn btn-normal" id="backBtn">关闭</a>
    </div>
  </div>
</form>
<script>
    
  var   editData,
        permission = CommonUtil.getPermission('${name_english}');
    
    $(function () {
    	editData = ${editData};
    	
    	
    	//填充上传图片list
   	 	$('#form').form().form('loadDataEdit', {
            data: editData
        }); 
    	
    	//初始化上传
    	filePlugin("#img","#imagesPicker","#imagesList");
    	
    	//初始化funcbar
     	$('#funcbar').prepend($.funcbar({
            type: 'button',
            data: permission,
            permit: ['${name_english}:edit']
        }));
   	 
     	$('#spuPlatform').combobox({
	           valueField : 'code',
	           textField : 'name',
	           url : COMMON_URL.getCode + '?type=T_DM_T_GOODS_SPU_SPU_PLATFORM'
	       });
	   	 $('#isHot').combobox({
	           valueField : 'code',
	           textField : 'name',
	           url : COMMON_URL.getCode + '?type=T_DM_T_GOODS_SPU_IS_HOT'
	       });
	   	 $('#isJingping').combobox({
	   	        valueField : 'code',
	   	        textField : 'name',
	   	        url : COMMON_URL.getCode + '?type=T_DM_T_GOODS_SPU_IS_JINGPING'
	   	    });
	   	 //T_DM_T_GOODS_SPU_WL_COMMISSION
	   	 $('#wlCommission').combobox({
	           valueField : 'code',
	           textField : 'name',
	           url : COMMON_URL.getCode + '?type=T_DM_WL_COMMISSION'
	       });
	   	 //T_DM_T_GOODS_SPU_WL_COMMISSION_SHARE
	   	 $('#wlCommissionShare').combobox({
	           valueField : 'code',
	           textField : 'name',
	           url : COMMON_URL.getCode + '?type=T_DM_T_GOODS_SPU_WL_COMMISSION_SHARE'
	       });
	   	 //T_DM_T_GOODS_SPU_CREATE_TIME
	   	 $('#createTime').combobox({
	           valueField : 'code',
	           textField : 'name',
	           url : COMMON_URL.getCode + '?type=T_DM_T_GOODS_SPU_CREATE_TIME'
	       });
	   	 
	   	//初始化下拉选项  商品所属公司
	   	 $('#categoryId').combobox({
	           url : CONST_URL.GET_CATEGORY_ID, 
	           valueField : 'id',
	           async:false,
	           textField : 'name'
	       });
	   	 //活动商品是否已选
	   	 $('#goodsCheck').combobox({
	           valueField : 'code',
	           textField : 'name',
	           url : COMMON_URL.getCode + '?type=T_DM_T_MARKET_GOODS_CHECK'
	       });
     	 //下拉初始化
   	    $('#isVisible').combobox({
   	    	async:false,
   	        defaultValue: editData.isVisible ,
            valueField : 'code',
            textField : 'name',
            url : COMMON_URL.getCode + '?type=T_DM_T_MARKET_BANNER_IS_VISIBLE'
        }); 
     	 
     	 $('#form').form({
        	onLoadSuccess:function(data){
               
           }
        }).form('loadDataEdit', {
            data: editData
        });
     	 
     	$('#type').combobox({
            valueField : 'code',
            textField : 'name',
            url : COMMON_URL.getCode + '?type=T_DM_T_MARKET_ADVERT_TYPE'
        });
    	   
     	 
     	$('#linkType').combobox({
	   		defaultValue: editData.linkType,
			async:false,
	        valueField : 'code',
	        textField : 'name',
	        url : COMMON_URL.getCode + '?type=T_DM_T_MARKET_LINK_TYPE',
	        onLoadSuccess: function () {
	        	if('h5'==editData.linkType){
	        		$("#happ").after('<div id="happvalue" class="form-cont">'+
	        		        '<p class="form-cont-title">H5模板</p>'+
	        		        '<input type="text" id="value" name="value" class="summer-input"/>'+
	        		     '</div>');
	         		$('#value').combobox({
	         			defaultValue: editData.value,
	         			async:false,
	         	        valueField : 'code',
	         	        textField : 'name',
	         	        url : COMMON_URL.getCode + '?type=T_DM_H5_TEMPLET'
	         	    });
	         	}
	         	if('app'==editData.linkType){
	         		$("#happ").after('<div id="happvalue" class="form-cont">'+
	        		        '<p class="form-cont-title">app模板</p>'+
	        		        '<input type="text" id="value" name="value" class="summer-input"/>'+
	        		     '</div>');
	         		$('#value').combobox({
	         			defaultValue: editData.value,
	         			async:false,
	         	        valueField : 'code',
	         	        textField : 'name',
	         	        url : COMMON_URL.getCode + '?type=T_DM_H5_TEMPLET'
	         	    });
	         	} 
	        },
	        onSelect: function(data){
	        	if('h5'==data.code){
	        		
	        		$("#happvalue").remove();
	        		
	        		$("#happ").after('<div id="happvalue" class="form-cont">'+
	        		        '<p class="form-cont-title">H5模板</p>'+
	        		        '<input type="text" id="value" name="value" class="summer-input"/>'+
	        		     '</div>');
	        		$('#value').combobox({
	        	        valueField : 'code',
	        	        textField : 'name',
	        	        url : COMMON_URL.getCode + '?type=T_DM_H5_TEMPLET'
	        	    });
	        	}
				if('app'==data.code){
	        		$("#happvalue").remove();
	        		
	        		$("#happ").after('<div id="happvalue" class="form-cont">'+
	        		        '<p class="form-cont-title">app模板</p>'+
	        		        '<input type="text" id="value" name="value" class="summer-input"/>'+
	        		     '</div>');
	        		$('#value').combobox({
	        	        valueField : 'code',
	        	        textField : 'name',
	        	        url : COMMON_URL.getCode + '?type=T_DM_APP_TEMPLET'
	        	    });
	        	}
	        }
	    });
     	  
    	//保存按钮
    	$('#funcbar').on('click', 'a[data-permit="${name_english}:edit"]', submit);
    	
    	//返回按钮
    	$('#backBtn, #goBack').on('click',backMain);
    	
    	
    	//初始化上传
        $("#img").filePlugin({
       	 type:'image'
        });
        $("#banner").filePlugin({
       	 type:'image'
        });
    	

    	// 查询按钮
        $('#search').on('click', reloadTable);
    	
        createTable();
        $('#reset').on('click', function() {
			//重置文本框的值
			$('#goodsName').val('');
			$('#spuPlatform').combobox('clear');
			$('#categoryId').combobox('clear');
			$('#isJingping').combobox('clear');
			$('#isHot').combobox('clear');
			$('#wlCommission').combobox('clear');
			$('#wlCommissionShare').combobox('clear');
			$('#createTime').combobox('clear');
			reloadTable();
		});
    });
    
 // 重新加载表格
	function reloadTable() {
		wlcReorder = $('#wlCommission').combobox('getValue');	
		wlcsReorder = $('#wlCommissionShare').combobox('getValue');
		timeReorder = $('#createTime').combobox('getValue');
		createTable(wlcReorder,wlcsReorder,timeReorder);
		$('#dataList').datagrid('reload', {
			ajax : {
				param : {
					"goodsName": trimSpace($('#goodsName').val()),
					"spuPlatform":$('#spuPlatform').combobox('getValue'),
					"isJingping": $('#isJingping').combobox('getValue'),
					"categoryId":$('#categoryId').combobox('getValue'),
					"goodsCheck":$('#goodsCheck').combobox('getValue'),
					"spuIds":$("#spuIds").val(),
					"isHot":$('#isHot').combobox('getValue')/* ,
					"wlCommission":$('#wlCommission').combobox('getValue'),
					"wlCommissionShare":$('#wlCommissionShare').combobox('getValue'),
					"createTime":$('#createTime').combobox('getValue') */
				}
			}
		});
	}
	
	//创建表格
	function createTable(wlcReorder,wlcsReorder,timeReorder) {
		
		$('#dataList').datagrid({
			ajax : {
				url : CONST_URL.GOODS_LIST+"?wlcReorder="+wlcReorder+"&wlcsReorder="+wlcsReorder+"&timeReorder="+timeReorder,
				param : {	
					"goodsName": trimSpace($('#goodsName').val()),
					 "spuPlatform":$('#spuPlatform').combobox('getValue'),
					"isJingping": $('#isJingping').combobox('getValue'),
					"categoryId":$('#categoryId').combobox('getValue'), 
					"goodsCheck":$('#goodsCheck').combobox('getValue'),
					"spuIds":$("#spuIds").val(),
					"isHot":$('#isHot').combobox('getValue')/* ,
					"wlCommission":$('#wlCommission').combobox('getValue'),
					"wlCommissionShare":$('#wlCommissionShare').combobox('getValue'),
					"createTime":$('#createTime').combobox('getValue') */
				},
				success : function(data) { //成功 
					alert("Data Saved: " + msg);
				}
			},
			columns : [
				{
    		    	 field: 'id',
    		    	 hidden: true
    		     },
    		     {
    		    	 field: 'goodsName'
    		     },
    		     {
    		    	 field: 'mainImageUrl',
    		    	 render: function (rowData) {
    		    		 return '<img style="height:100px;width:100px;" src="'+ rowData.mainImageUrl +'"></img>';
    		    	 }
    		     },
    		     {
    		    	 field: 'spuPlatform'
    		     },
    		     {
    		    	 field: 'price'
    		     },
    		     {
    		    	 field: 'couponMoney'
    		     },
    		     {
    		    	 field:'conPrice'
    		     },
    		     {
    		    	 field:'wlCommissionShare'
    		     },
    		     {
    		    	 field:'wlCommission'
    		     },
    		     {
    		    	 field:'note'
    		     },
					{
	    		    	 field:'anniu',
	    		    	 render: function (rowData) {
	    		    		 
	    		    		 var ids = $("#spuIds").val();
	    		    		 var id = rowData.id;
	    		    		 
	    		    		 if(ids.indexOf(id)!=-1 ){
							    return '<input type="button" class="setHd" value="取消活动"></input>';
							}else{
		    		    		 return '<input type="button" class="setHd" value="设置活动"></input>';
							}
	    		    	 }
					}

			]
		});
	}
    
    //设置活动、取消活动
    $('#t-wrap').on('click', '.setHd', function() {
    	var $this = $(this);
        var id = $this.closest('tr').find('td[data-field="id"]').attr('data-value');
        if($(this).val()=="设置活动"){
        	 var ids = $("#spuIds").val();
             ids += ","+id;
             if (ids.substr(0,1)==','){
     			ids=ids.substr(1);
     		 }
             $("#spuIds").val(ids);
             $(this).val("取消活动");
        }else if($(this).val()=="取消活动"){
        	  var ids = $("#spuIds").val();
              var idnew = remove(ids,id)
              $("#spuIds").val(idnew);
              $(this).val("设置活动");
        }
    });
    
    //在某个字符串中删除指定字符串
    function remove(a,b){
        var c=a.split(','),d="";
        for(var i=0;i<c.length;i++){
            d+=c[i]==b?"":","+c[i];
        }
        return d.length>0?d.substring(1):"";
    }
    
    
    //表单提交
    function submit() {
         $("#form").form({
        	 buttons : ['#saveBtn'],
             ajax: {
                 url: CONST_URL.EDIT
             },
             onSubmitSuccess: function (data) {
            	 $.stip({
   	                type:'ok',//显示类型 有info、 ok、 danger
   	                text: '保存成功！',
   	                onBeforeHide: function () {
   	                	backMain();
   	                }
            	 });
              }
         }).form("submit");
    }
    
    //返回主页面
    function backMain() {
    	window.location.href = CONST_URL.INIT_MAIN;
    }
    
    //请求url
    var CONST_URL = {
    	LIST: basePath + "/mall/system/goods/goodsspu/list",
    	INIT_MAIN: basePath + "${common_url}init",
    	ET_CATEGORY_ID: basePath + '/mall/system/market/marketshare/getGoodsCategory',
    	GOODS_LIST: basePath + '/mall/system/market/marketshare/goodsList',
       	EDIT: basePath + "${common_url}edit"
    };
    
</script>
</body>
</html>